package com.aqie.medium.dfs;

import com.aqie.easy.structure.TreeNode;

/**
 * 105 前序与中序遍历构造二叉树
 * 树中没有重复的元素
 * 前序：根 左 右
 * 中序：左 根 右
 */
public class BuildTree {
    /**
     * 1，递归遍历inorder 19ms
     * @param preorder
     * @param inorder
     * @return
     */
    public TreeNode buildTree(int[] preorder, int[] inorder) {

        return buildTree(preorder,inorder,new int[1],0,inorder.length - 1);
    }

    private TreeNode buildTree(int[] preorder,int[] inorder,int[] preindex,int begin,int end){
        if(preindex[0] > preorder.length - 1){
            return null;
        }
        TreeNode node = new TreeNode(preorder[preindex[0]]);
        if(begin > end){
            return null;
        }
        int inorderindex = 0;
        for(int i = begin;i<= end;i++){
            if(inorder[i] == preorder[preindex[0]]){
                inorderindex = i;
                break;
            }
        }
        preindex[0] = preindex[0] + 1;
        TreeNode left = buildTree(preorder,inorder,preindex,begin,inorderindex - 1);
        TreeNode right = buildTree(preorder,inorder,preindex,inorderindex + 1,end);
        node.left = left;
        node.right = right;
        return node;
    }

    /**
     * 递归 32ms
     * @param preorder
     * @param inorder
     * @return
     */
    public TreeNode buildTree2(int[] preorder, int[] inorder) {
        return buildTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
    }

    private TreeNode buildTree(int[] preorder, int ps, int pe, int[] inorder, int is, int ie) {
        if (ps > pe) {
            return null;
        }
        // 前序遍历第一个节点为根节点
        int rootVal = preorder[ps];
        TreeNode root = new TreeNode(rootVal);
        // 确定根节点在中序遍历中的位置
        int im = is;
        while (im < ie && inorder[im] != rootVal) {
            im++;
        }
        // 根据根节点在中序遍历中的位置得到左右子树的前序遍历和中序遍历序列
        root.left = buildTree(preorder, ps + 1, ps + im - is, inorder, is, im - 1);
        root.right = buildTree(preorder, ps + im - is + 1, pe, inorder, im + 1, ie);
        return root;
    }


    // “归并排序”和“快速排序”都是分治法思想的应用

    /**
     * O(N) 17ms
     * @param preorder
     * @param inorder
     * @return
     */
    public TreeNode buildTree3(int[] preorder, int[] inorder) {
        int preLen = preorder.length;
        int inLen = inorder.length;
        return helper(preorder, 0, preLen - 1, inorder, 0, inLen - 1);
    }


    private TreeNode helper(int[] preorder,
                            int preL, int preR,
                            int[] inorder,
                            int inL, int inR) {
        if (preL > preR || inL > inR) {
            return null;
        }
        int rootVal = preorder[preL];
        int l = inL;
        while (l <= inR && inorder[l] != rootVal) {
            l++;
        }
        TreeNode root = new TreeNode(rootVal);
        root.left = helper(preorder, preL + 1, preL + l - inL, inorder, inL, l - 1);
        root.right = helper(preorder, preL + l - inL + 1, preR, inorder, l + 1, inR);
        return root;
    }


}
